package com.visizen.wisdomclass.server.action.User;

import com.alibaba.fastjson.JSONObject;
import com.visizen.wisdomclass.server.action.response.ErrorCode;
import com.visizen.wisdomclass.server.action.response.ErrorCodeManager;
import com.visizen.wisdomclass.server.action.response.Result;
import com.visizen.wisdomclass.server.annotation.ValidateToken;
import com.visizen.wisdomclass.server.model.WcUser;
import com.visizen.wisdomclass.server.service.UserService;
import com.visizen.wisdomclass.server.token.WCToken;
import com.visizen.wisdomclass.server.token.WCTokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 【用户】 修改用户密码
 *
 * @author: chenjian
 */
@Controller
@RequestMapping("/User")
public class UserChangePasswordAction {
    /**
     * 修改用户的密码
     *
     * @param token
     * @param oldpassmd5 当前的密码md5值
     * @param newpassmd5 新密码的md5值
     * @param equipUUID  修改密码时所使用的设备UUID
     * @param request
     * @return
     */
    @ValidateToken
    @RequestMapping(value = "/changeUserPassword", method = RequestMethod.POST)
    @ResponseBody
    public Result changePassword(@RequestParam(required = true) String token,
                                 @RequestParam(required = true) String oldpassmd5,
                                 @RequestParam(required = true) String newpassmd5,
                                 @RequestParam(required = false) String equipUUID,
                                 HttpServletRequest request) {
        Result result = new Result();
        JSONObject jo = new JSONObject();
        WCToken wcToken = tokenManager.getToken(token);

        WcUser user = (WcUser) wcToken.getSource();
        /**
         * 判断原密码是否正确
         */
        if (null == oldpassmd5 || "".equals(oldpassmd5) || !oldpassmd5.equals(user.getPassmd5())) {
            /**
             * 原密码错误
             */
            jo.put("exception", "");
            ErrorCodeManager codeManager = ErrorCodeManager.getLocalMsg(ErrorCode.C1021_oldPassmd5Wrong);
            result.setFailedResult(request, codeManager.getCode(), codeManager.getMsg(), jo);
        } else {
            /**
             * 原密码正确，进行密码修改
             */
            boolean isSucc = userService.changeUserPassmd5(user.getUser_id(), newpassmd5);
            /**
             * 密码是否修改成功
             */
            if (isSucc) {
                /**
                 * 密码修改成功，更新token中的对象
                 */
                WcUser newUser = userService.getUserByUsername(user.getUsername());
                wcToken.setSource(newUser);
                tokenManager.updateToken(token, wcToken);
                result.setSuccessResult(request, "", null);
            } else {
                /**
                 * 密码修改失败
                 */
                jo.put("exception", "");
                ErrorCodeManager codeManager = ErrorCodeManager.getLocalMsg(ErrorCode.C1022_changePassmd5Failed);
                result.setFailedResult(request, codeManager.getCode(), codeManager.getMsg(), jo);
            }
        }
        return result;
    }

    private WCTokenManager tokenManager;
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setTokenManager(WCTokenManager tokenManager) {
        this.tokenManager = tokenManager;
    }
}
